package document.controller;


import document.dao.DocumentDao;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DocumentUrlGetServlet extends HttpServlet {

    private final DocumentDao documentDao = new DocumentDao();
    private static final long serialVersionUID = 1;

    public DocumentUrlGetServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    /**
     * 获取文档url链接，以便能够进行文档查看和下载
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        System.out.println("进入了DocumentDownloadServlet");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int documentId = Integer.parseInt(request.getParameter("id"));

        String url = documentDao.getDocumentUrlById(documentId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("code",200);
        if(url == null) {
            jsonObject.put("message","文件上传路径不存在");
        } else {
            jsonObject.put("message","获取文件上传路径成功");
        }
        System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
